package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.dbmodel.Warehousing;
import com.esm.sm800.database.greendao.MemberDao;
import com.esm.sm800.database.greendao.WarehousingDao;

import java.util.ArrayList;

public class WarehousingCenter {

    private static WarehousingCenter instance = new WarehousingCenter();

    public static WarehousingCenter getInstance() {
        return instance;
    }

     public Warehousing getWarehousing(String position) {
        Warehousing Warehousing = null;
        try {
            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            ArrayList<Warehousing> list = new ArrayList<Warehousing>();
            list = (ArrayList) WarehousingDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.WarehousingDao.Properties.Position.eq(position))
                    .build().list();
            if (list.size() > 0) {
                Warehousing = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Warehousing;
    }

    public Long saveWarehousing(Warehousing Warehousing) {
        if (Warehousing == null) {
            return 0L;
        }

        try {
            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            long id = WarehousingDao.insertOrReplace(Warehousing);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public Long insertWarehousing(Warehousing Warehousing) {
        if (Warehousing == null) {
            return 0L;
        }

        try {
            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            long id = WarehousingDao.insertOrReplace(Warehousing);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public ArrayList<Warehousing> warehousinglist = null;

    public ArrayList<Warehousing> getWarehousinglist() {
        ArrayList<Warehousing> list = new ArrayList<Warehousing>();
        try {
            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            list = (ArrayList) WarehousingDao.queryBuilder().build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public boolean delWarehousing(Warehousing warehousing) {
        if (warehousing == null) {
            return false;
        }

        try {
            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            WarehousingDao.delete(warehousing);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }
}
